/**
 * @author Stream
 * @date 2012 Jul 21, 2012 6:15:12 PM
 */
package com.boronstream.istar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 
 * 
 */
public class SModel extends Object {
	/**
	 * xml layout file name
	 */
	private String xmlName = "";

	/**
	 * layout type: activity , item ,etc.
	 */
	private SLayoutEnum layoutType;
	/**
	 *  */
	private String generatedName = "";

	private boolean themeTitleBarUsed;
	/**
	 *
	 */
	private HashMap<String, String> javaFields = new HashMap<String, String>();

	/**
	 * 
	 */
	private String contentString = "";

	/**
	 * @return the generatedName
	 */
	public String getGeneratedClassName() {
		return generatedName;
	}

	/**
	 * 
	 * @return xmlName
	 */
	public String getXMLName() {
		return xmlName;
	}

	/**
	 * @param xmlName
	 *            XML with out .xml:Layout+ xmlName+Activity
	 */
	public void setXMLName(String xmlName) {
		this.xmlName = xmlName;
		// xmlName = xmlName.replace("activity", "");
		String newName = this.relpace_ToUperChar(xmlName);
		// newName = "Layout" + newName + "Activity";
		this.generatedName = newName;
	}

	/**
	 * @return the StreamField
	 */
	private List<StreamField> getJavaFields() {
		if (this.javaFields == null) {
			return null;
		}
		List<StreamField> fields = new ArrayList<SModel.StreamField>();
		Set<String> sets = javaFields.keySet();
		for (String id : sets) {
			String className = javaFields.get(id);// get field type

			StreamField streamField = new StreamField();
			streamField.id += id;// like R.string.app_name;
			streamField.className = className;
			streamField.name += relpace_ToUperChar(id);// so this will be
														// like:mTextView....
			fields.add(streamField);
		}
		return fields;
	}

	/**
	 * @param javaFields
	 * <br>
	 *            HashMap?? * ???:<???????id,?????????????????>
	 */
	public void setJavaFields(HashMap<String, String> javaFields) {
		this.javaFields = javaFields;
		this.setContentString();
	}

	/**
	 * @return the contentString
	 */
	public StringBuffer getContentString() {
		return new StringBuffer(contentString);
	}

	/**
	 * @param contentString
	 *            the contentString to set
	 */
	private void setContentString() {

		// Step1.Get all the java fields
		List<StreamField> streamFields = this.getJavaFields();
		if (streamFields == null) {
			return;
		}

		// Step2.
		StringBuffer block = new StringBuffer("");
		// --------------ClassModifer--------------//
		String classModifer = " class ";
		if (this.getLayoutType() == SLayoutEnum.ITEM
				|| this.getLayoutType() == SLayoutEnum.TITLEBAR) {
			classModifer = "	public class ";
		}
		// --------------Block1--------------//
		// String block1 = classModifer + this.getGeneratedClassName()
		// + " extends " + layoutType.typeName + " {\r\n";
		// if (this.getLayoutType() == SLayoutEnum.TITLEBAR) {
		String block1 = classModifer + this.getGeneratedClassName() + " {\r\n";
		// }

		// --------------Block2--------------//
		// String block2 = "		View sTitleBarView;\r\n"
		// + "		View sContentView;\r\n";
		String block2 = "";

		// --------------Block3--------------//
		String block3 = "";
		if (this.getLayoutType() == SLayoutEnum.ACTIVITY
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYS
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYF) {
			// block3 = "\r\n		@Override\r\n"
			// + "		protected void onCreate(Bundle savedInstanceState) {\r\n"
			// +
			// "          setTheme(android.R.style.Theme_Translucent_NoTitleBar);\r\n"
			// +
			// "         getWindow().setWindowAnimations(android.R.style.Animation_Activity);\r\n"
			// + "			super.onCreate(savedInstanceState);\r\n"
			// + "			setContentView(R.layout."
			// + this.getXMLName()
			// + ");\r\n"
			// +
			// "          getWindow().setBackgroundDrawableResource(R.drawable.bg);\r\n\r\n";
			block3 = "\r\n		public " + this.getGeneratedClassName()
					+ "(Activity activity) {\r\n";
		} else if (this.getLayoutType() == SLayoutEnum.ITEM
				|| this.getLayoutType() == SLayoutEnum.TITLEBAR) {
			block3 = "\r\n		public " + this.getGeneratedClassName()
					+ "(View convertView) {\r\n";
		}

		// --------------Block4--------------//
		String block4 = "";
		if (this.getLayoutType() == SLayoutEnum.ACTIVITY
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYS
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYF) {
			block4 =
			// "			// //Step1.new a new layout :\r\n"
			// + "			LinearLayout sLinearLayout = new LinearLayout(this);\r\n"
			// + "			sLinearLayout.setOrientation(LinearLayout.VERTICAL);\r\n"
			// + "			sLinearLayout.setLayoutParams(new LayoutParams(\r\n"
			// +
			// "					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));\r\n\r\n"
			// +
			//
			// "			// Step2.read 'TitleBar' and 'contentView' from XML file\r\n"
			// +
			// "			LayoutInflater sInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r\n"
			// + "			sTitleBarView = sInflater.inflate(R.layout."
			// + SGenerate.XML_PRIFIX
			// + "_titlebar, null);\r\n"
			// + "			sContentView = sInflater.inflate(R.layout."
			// + this.getXMLName()
			// + ", null);\r\n"
			// + "			final "
			// + SGenerate.XML_PRIFIX_C
			// + "Titlebar "
			// + SGenerate.XML_PRIFIX
			// + "Titlebar = new "
			// + SGenerate.XML_PRIFIX_C
			// + "Titlebar(sTitleBarView);\r\n\r\n"
			// +
			//
			// "			// Step3.put the two view into a 'layout'\r\n"
			// + "			sLinearLayout.addView(sTitleBarView, new LayoutParams(\r\n"
			// +
			// "					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));\r\n"
			// + "			sLinearLayout.addView(sContentView, new LayoutParams(\r\n"
			// +
			// "					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));\r\n\r\n"
			// +

			"			// Step1.setContentView:\r\n"
					+ "			activity.setContentView(R.layout."
					+ this.getXMLName() + ");\r\n" +

					"			//  Step2.initlize the property views;\r\n";
		}
		// --------------Block5--------------//
		String block5 = "		}\r\n" + "	}\r\n";

		if (this.getLayoutType() == SLayoutEnum.ITEM
				|| this.getLayoutType() == SLayoutEnum.TITLEBAR) {
			block2 = "";
		}

		// Step3.Add fields definition into java code

		for (StreamField streamField : streamFields) {
			if (this.getLayoutType() == SLayoutEnum.ITEM) {
				block2 += "     public";
			}
			if ("include".equals(streamField.className)) {
				continue;
			}
			block2 += "		" + streamField.className + " " + streamField.name
					+ ";\r\n";

			String findString = "activity";
			if (streamField.id.startsWith("R.id.s_titlebar_")) {
				findString = "sTitleBarView";
			}

			if (this.getLayoutType() == SLayoutEnum.ACTIVITY
					|| this.getLayoutType() == SLayoutEnum.ACTIVITYS
					|| this.getLayoutType() == SLayoutEnum.ACTIVITYF) {
				block4 += "			" + streamField.name + "=("
						+ streamField.className + ")" + findString
						+ ".findViewById(" + streamField.id + ");\r\n";
			} else if (this.getLayoutType() == SLayoutEnum.ITEM
					|| this.getLayoutType() == SLayoutEnum.TITLEBAR) {
				block4 += "			" + streamField.name + "=("
						+ streamField.className + ")convertView.findViewById("
						+ streamField.id + ");\r\n";
			}
		}

		if (this.getLayoutType() == SLayoutEnum.ACTIVITY
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYS
				|| this.getLayoutType() == SLayoutEnum.ACTIVITYF) {
			// block4 += "					// Step6.initlize the TitleBar\r\n"
			// + "					"
			// + SGenerate.XML_PRIFIX
			// + "Titlebar.mSTitlebarTitle.setText(getTitle());\r\n\r\n"
			// +
			// //
			// "					"
			// + SGenerate.XML_PRIFIX
			// +
			// "Titlebar.mSTitlebarLeftbutton.setVisibility(View.INVISIBLE);\r\n\r\n"
			// +
			// //
			// "					"
			// + SGenerate.XML_PRIFIX
			// + "Titlebar.mSTitlebarLeftbutton\r\n"
			// +
			// "							.setOnClickListener(new View.OnClickListener() {\r\n\r\n"
			// +
			// //
			// "								public void onClick(View v) {\r\n"
			// + "									onBackPressed();\r\n" + "								}\r\n"
			// + "							});\r\n";
		}

		block.append(block1);
		block.append(block2);
		block.append(block3);
		block.append(block4);
		block.append(block5);

		// Step4.Generate the contentString
		this.contentString = block.toString();
	}

	/**
	 * @return the layoutType
	 */
	public SLayoutEnum getLayoutType() {
		return layoutType;
	}

	/**
	 * @return the themeTitleBarUsed
	 */
	public boolean isThemeTitleBarUsed() {
		return themeTitleBarUsed;
	}

	/**
	 * @param themeTitleBarUsed
	 *            the themeTitleBarUsed to set
	 */
	public void setThemeTitleBarUsed(boolean themeTitleBarUsed) {
		this.themeTitleBarUsed = themeTitleBarUsed;
	}

	/**
	 * @param layoutType
	 *            the layoutType to set
	 */
	public void setLayoutType(SLayoutEnum layoutType) {
		this.layoutType = layoutType;
	}

	/**
	 * 
	 * @author Stream
	 * 
	 */

	public class StreamField {
		String name = "m";// field is started with m prefix
		String className = "";
		String id = "R.id.";// R.id.xxx
	}

	/**
	 * 
	 * @param xmlName
	 *            activity_layout_me
	 * @return ActivityLayoutMe
	 */

	private String relpace_ToUperChar(String xmlName) {
		String[] nameArray = xmlName.split("_");
		String newName = "";
		for (String name : nameArray) {
			newName += name.replaceFirst(name.charAt(0) + "",
					(name.charAt(0) + "").toUpperCase());
		}
		return newName;
	}

	/**
	 * 
	 * Layout
	 * 
	 */
	public enum SLayoutEnum {
		/**
		 * Activity
		 */
		ACTIVITY(Gen.XML_PRIFIX + "_activity_", "Activity"),

		/**
		 * Item: activity group
		 */
		ACTIVITYF(Gen.XML_PRIFIX + "_activityf_", "FragmentActivity"),

		/**
		 * Item: activity group
		 */
		ACTIVITYS(Gen.XML_PRIFIX + "_activitys_", "ActivityGroup"),

		/**
		 * Item:ListView ,etc
		 */
		ITEM(Gen.XML_PRIFIX + "_item_", "Object"),

		/**
		 * Activity titlebar
		 */
		TITLEBAR(Gen.XML_PRIFIX + "_titlebar", "");

		SLayoutEnum(String typeNative, String typeName) {
			this.typeNative = typeNative;
			this.typeName = typeName;
		}

		public final String typeName;
		public final String typeNative;
	}
}
